﻿using Sys.Common;
using Sys.Core.Sys;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Api.Sys
{
    public class DepartmentApi : BaseApi
    {
        public string Grid()
        {
            var entities = DepartmentBll.I.SortEntities;

            return JsonHelper.ToJson(entities.ToList());
        }

        public string Tree()
        {
            var entities = DepartmentBll.I.SortEntities;

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));

            return CustomJson(entities.ToList(), jms, false);
        }

        public string FilterTree()
        {
            var list = DepartmentBll.I.SortEntities.ToList();

            var model = DepartmentBll.I.NullModel();

            model.Name = "查看所有";

            list.Insert(0, model);

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));

            return CustomJson(list, jms, false);
        }

        public string ComboTree()
        {
            var list = DepartmentBll.I.SortEntities.ToList();

            list.Insert(0, DepartmentBll.I.NullModel());

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));

            return CustomJson(list, jms, false);
        }

        public string Save(SysDepartment model)
        {
            if (model.Parent.Equals("Null")) { model.Parent = string.Empty; }

            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.IntSort = DepartmentBll.I.MaxSort(m => m.IntSort);

                result.Success = DepartmentBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                result.Success = DepartmentBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = DepartmentBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string SetOrder(string id, string parent, string order)
        {
            JsonMsg result = new JsonMsg();

            var entities = DepartmentBll.I.Entities.Where(m => m.Parent.Equals(parent));

            bool success = false;

            result.Message = DepartmentBll.I.SetOrder(entities, id, "IntSort", order, out success);

            result.Success = success;

            return JsonHelper.ToJson(result);
        }
    }
}
